package com.ruoyi.lab.aop;

import com.ruoyi.lab.domain.LabAppointMessage;
import com.ruoyi.lab.domain.LabSeatOperate;
import com.ruoyi.lab.enums.OperateEnum;
import com.ruoyi.lab.service.ILabAppointMessageService;
import com.ruoyi.lab.service.ILabSeatOperateService;
import com.sun.org.apache.bcel.internal.generic.RETURN;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

@Component
@Aspect
@Slf4j
public class SeatAspect {

    @Autowired
    private ILabSeatOperateService labSeatOperateService;

    @Autowired
    private ILabAppointMessageService labAppointMessageService;

    @Pointcut("@annotation(com.ruoyi.lab.annotation.SeatOperate)")
    public void pointcut() {}

    @Around("pointcut()")
    @Transactional(rollbackFor = Exception.class)
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        LabSeatOperate labSeatOperate = new LabSeatOperate();
        Object[] args = joinPoint.getArgs();
        boolean isAdd = false; // 记录是添加操作还是修改操作
        for (Object arg : args) {
            // 如果参数是实体，表示添加或修改操作
            if (arg instanceof LabAppointMessage) {
                LabAppointMessage appointMessage = (LabAppointMessage) arg;
                if (appointMessage.getAppointId() == null) {
                    // 方法未执行，没有主键，则是添加操作
                    isAdd = true;
                    labSeatOperate.setOperateStatus(OperateEnum.OPERATE_APPOINT.getStatus()); // 标记预约操作
                } else {
                    labSeatOperate.setOperateStatus(OperateEnum.OPERATE_EDIT.getStatus()); // 标记修改操作
                }
                labSeatOperate.setSeatId(appointMessage.getSeatId());
                labSeatOperate.setStartAppointTime(appointMessage.getStartAppointTime());
                labSeatOperate.setEndAppointTime(appointMessage.getEndAppointTime());
            } else if (arg instanceof Long[]) {
                // 如果参数是Long，表示删除操作，取消预约
                labSeatOperate.setOperateStatus(OperateEnum.OPERATE_CANCEL.getStatus()); // 标记删除操作

                Long[] appointIds = (Long[]) arg;
                for (Long appointId : appointIds) {
                    LabAppointMessage appointMessage = labAppointMessageService.selectLabAppointMessageByAppointId(appointId);
                    labSeatOperate.setSeatId(appointMessage.getSeatId());
                    labSeatOperate.setStartAppointTime(appointMessage.getStartAppointTime());
                    labSeatOperate.setEndAppointTime(appointMessage.getEndAppointTime());
                }
            }
        }

        Object result = joinPoint.proceed();

        for (Object arg : args) {
            // 如果参数是实体，表示添加或修改操作
            if (arg instanceof LabAppointMessage) {
                LabAppointMessage appointMessage = (LabAppointMessage) arg;

                // 对于添加操作
                if (isAdd && appointMessage.getAppointId() == null) {
                    // 如果主键为空，表示没有添加成功，预约失败
                    log.error("预约失败，不记录日志");
                    return result;
                }

                // 对于修改操作
                if (!isAdd && appointMessage.getUpdateBy() == null) {
                    // 如果修改者为空，表示没有修改成功
                    log.error("修改失败，不记录日志");
                    return result;
                }
            }
        }

        labSeatOperateService.insertLabSeatOperate(labSeatOperate);
        return result;
    }
}
